

#ifndef ADQUERY_H_
#define ADQUERY_H_


#include "HTTPUtils.h"
#include "CActiveHelper.h"
#include <euserhl.h>
#include "ActiveTarget.h"
#include "imageconversion.h"
#include <http.h>
#include <bitmaptransforms.h>
#include <es_sock.h>
#include <lbsposition.h>


struct CAdBanner : public CBase
	{
	HBufC8* iURL;
	CFbsBitmap* iBitmap;
	~CAdBanner();
	};

class CAdSession;

struct CAdQuery : public CBase
{
CONSTRUCTORS_MAY_LEAVE
	typedef scalp::CFunction< void(CAdQuery*, TInt aStatusCode, TInt aErrorCode, CAdBanner*) > TCallback;
	LManagedPtr< TCallback > iCallback;
	RHTTPTransaction iTransaction;	
	LManagedHandle<RTimer> iTimeoutTimer;
	LManagedPtr<scalp::CActiveTarget> iTimeoutTimerTarget;
	void TransactionTimeoutCallbackL();
	LManagedPtr<CHTTPTransactionTarget> iTransactionTarget;
	LManagedPtr<CImageDecoder> iImageDecoder;
	LManagedPtr<scalp::CActiveTarget> iImageDecoderTarget;
	LManagedPtr<CBitmapScaler> iBitmapScaler;
	LManagedPtr<scalp::CActiveTarget> iBitmapScalerTarget;
	CAdSession* iSession;
	LManagedPtr<CAdBanner> iAdBanner;
	TCoordinate iCoordinate;
	TBool iUseCoordinate;
	
	CAdQuery(CAdSession* aSession);
	TBool Pending() const;
	void SetCoordinate(const TCoordinate& aCoordinate);
	void SubmitL(TCallback* aCallback);
	void Cancel();
	~CAdQuery();
};


class CAdSession : public CBase
	{
	
RArray<CAdQuery*> iAdQueries;
friend class CAdQuery;
public:
	// constructors and destructor
	CAdSession();
	static CAdSession* NewL();
	static CAdSession* NewLC();        
	void ConstructL();
	virtual ~CAdSession();
	TInt iStatusCode;
	
	
	RHTTPSession iHTTPSession;
	HBufC8* iClientId;
	void SetConnectionInfoL(const RConnection& aConnection, const RSocketServ& aSocketServ);
	void CloseConnectionL();
	void NotifyConnected();
	
	void ReceivedAdAnswerL(CAdQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedAdImageAnswerL(CAdQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	TInt AdTransactionError(CAdQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);
	void ImageDecoded(CAdQuery* aQuery);
	void ImageScaled(CAdQuery* aQuery);
	
	void SubmitL(CAdQuery* aQuery);
	void Cancel(CAdQuery* aQuery);
	};

#endif /* ADQUERY_H_ */
